//
//  StoryViewController.m
//  TrueLove
//
//  Created by 刘康 on 15/1/29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StoryViewController.h"
#import "MyStoryCell.h"
#import "BrideSayViewController.h"
#import "StoryDetailViewController.h"
#import "AFNetworking.h"
#import "Story.h"
#import "UIImageView+WebCache.h"
@interface StoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain)NSMutableArray *array;


@property(nonatomic, retain)UITableView *tableView;

@end

@implementation StoryViewController

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.hidesBackButton = YES;
    [self createSegmentedControl];
    [self createTableView];
    [self netWrokingForStory];
    
}

//网络请求
- (void)netWrokingForStory
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manager GET:@"http://www.hunliji.com/stories.json?sort=0&month=0&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        NSArray *arr = [dic objectForKey:@"stories"];
        self.array = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            Story *story = [[Story alloc] init];
            [story setValuesForKeysWithDictionary:dic];
            [self.array addObject:story];
            [story release];
            [_tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

//创建segmentControl
- (void)createSegmentedControl
{
    NSArray *arr = [NSArray arrayWithObjects:@"小组",@"故事", nil];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:arr];
    self.navigationItem.titleView = segment;
    segment.tintColor = [UIColor redColor];
    segment.selectedSegmentIndex = 1;//给一个默认选中的段
    [segment addTarget:self action:@selector(controllerPressed:) forControlEvents:UIControlEventValueChanged];

}

//segment关联的方法
- (void)controllerPressed:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
    [self.navigationController popViewControllerAnimated:NO];
    }
}

- (void)createTableView
{
    self.navigationController.navigationBar.translucent = NO;
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 250;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *str = @"reues";
        MyStoryCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[MyStoryCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
        }
    
    //取消选中的状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    Story *story = [self.array objectAtIndex:indexPath.row];
    
    [cell.MyImageView setImageWithURL:[NSURL URLWithString:story.cover_path]];
    [cell.userImageView setImageWithURL:[NSURL URLWithString:[story.user objectForKey:@"avatar"]]];
    cell.userNameLabel.text = [story.user objectForKey:@"nick"];
    cell.UserTitleLabel.text = story.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StoryDetailViewController *storyDetailVC = [[StoryDetailViewController alloc] init];
    [self.navigationController pushViewController:storyDetailVC animated:YES];
    [storyDetailVC release];
}


    
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
